package com.eclink.iot.rfid;

import com.eclink.iot.DeviceOnlineStatusListener;
import com.eclink.iot.base.SocketClientBase;
import com.eclink.utils.Log;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class RFIDServer
{
  private static String TAG = "RFIDServer"; private static ServerSocket server; private static RFIDTagDataListener rfid_tag_data_listener;
  private static DeviceOnlineStatusListener device_online_status_listener;
  private static Map<String, RFIDDeviceClient> devices = new HashMap<>();
  private static Runnable process;
  private static SocketClientBase.ConnectStatusChangeListener connect_status_change_listener;
  
  public static boolean initialization(int port) {
    try {
      server = new ServerSocket(port);
      Log.d(TAG, "Server started. Waiting for clients...");
      
      (new Thread(process)).start();
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    } 
    
    return true;
  }
  static {
    process = (() -> {
        while (true) {
          try {
            while (true) {
              Socket clientSocket = server.accept();
              (new RFIDDeviceClient(clientSocket, connect_status_change_listener)).start();
              Thread.sleep(10L);
            } 

          } catch (Exception e) {
            e.printStackTrace();
          } 
        } 
      });
    
    connect_status_change_listener = ((object, uuid, status) -> {
        RFIDDeviceClient client = (RFIDDeviceClient)object;
        if (status) {
          if (devices.containsKey(uuid)) {
            devices.remove(uuid);
          }
          devices.put(uuid, client);
          client.set_rfid_tag_data_listener(rfid_tag_data_listener);
          if (device_online_status_listener != null) {
            device_online_status_listener.notice(uuid, true);
          }
        } else {
          if (devices.containsKey(uuid)) {
            devices.remove(uuid);
          }
          if (device_online_status_listener != null) {
            device_online_status_listener.notice(uuid, false);
          }
        } 
      });
  }
  
  public static void set_printf(boolean enable) {
    Log.set_enable(enable);
  }
  
  public static void set_rfid_tag_data_listener(RFIDTagDataListener listener) {
    rfid_tag_data_listener = listener;
  }
  
  public static void set_device_online_status_listener(DeviceOnlineStatusListener listener) {
    device_online_status_listener = listener;
  }
  
  public static List<String> get_online_devices() {
    return new ArrayList<>(devices.keySet());
  }
  
  public static boolean reboot(String uuid) {
    if (devices.containsKey(uuid)) {
      return ((RFIDDeviceClient)devices.get(uuid)).reboot().booleanValue();
    }
    
    return false;
  }
  
  public static boolean inventory(String uuid, int ant) {
    if (devices.containsKey(uuid)) {
      return ((RFIDDeviceClient)devices.get(uuid)).inventory(ant);
    }
    
    return false;
  }
  
  public static boolean inventory_multi(String uuid, int ant) {
    if (devices.containsKey(uuid)) {
      return ((RFIDDeviceClient)devices.get(uuid)).inventory_multi(ant);
    }
    
    return false;
  }
  
  public static boolean stop(String uuid) {
    if (devices.containsKey(uuid)) {
      return ((RFIDDeviceClient)devices.get(uuid)).stop_inventory();
    }
    
    return false;
  }

  public static boolean upgrade(String uuid) {
    if (devices.containsKey(uuid)) {
      byte[] data = new byte[1024];
      return devices.get(uuid).upgrade(data);
    }

    return false;
  }
}


/* Location:              E:\code\work\eclink\eclink-iot-tcp-server\backend\libs\eclink-iot.jar!\com\eclink\iot\rfid\RFIDServer.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */